package org.ss.psci.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.ss.psci.entity.PmAddressList;
import org.ss.psci.entity.PmAssignment;

/**
 * Data access object (DAO) for domain model class PmAssignment.
 * 
 * @see org.ss.psci.entity.PmAssignment
 * @author MyEclipse Persistence Tools
 */

public class PmAssignmentDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(PmAssignmentDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String REMARK = "remark";
	public static final String PARENT = "parent";
	public static final String USERNAME = "username";
	public static final String START_TIME = "startTime";
	public static final String END_TIME = "endTime";
	public static final String PLAN_DAYS = "planDays";
	public static final String QUANTITY = "quantity";
	public static final String COMPANY = "company";
	public static final String MANAGER = "manager";
	public static final String PRIORITY = "priority";
	public static final String STATUS = "status";

	public static final String COMPANYID = "companyId";
	public static final String PROJECTID = "projectId";
	public static final String CONSID = "consId";

	public List findByProjectIDAndConsId(Object projectId, Object consId) {
		return findByProperty(PROJECTID, projectId, CONSID, consId);
	}

	public List findAssIdByPIDAndCId(Object projectId, Object consId,
			Object assid) {
		try {
			String queryString = "from PmAssignment as model where model.projectId=? and  model.consId=? and "
					+ "model.assid like " + assid + "% order by model.assignmentId";
			return getHibernateTemplate().find(queryString,
					new Object[] { projectId, consId });
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByConsID(Object name) {
		return findByProperty(CONSID, name);
	}

	public List findByCompanyID(Object name) {
		return findByProperty(COMPANYID, name);
	}

	public List findByProjectId(Object name) {
		return findByProperty(PROJECTID, name);
	}

	public PmAssignment copyEntity(PmAssignment dest, PmAssignment orig) {
		if (dest == null || orig == null) {
			return new PmAssignment();
		}
		if (orig.getPriority() != null) {
			dest.setPriority(orig.getPriority());
		}
		if (orig.getStatus() != null) {
			dest.setStatus(orig.getStatus());
		}
		if (orig.getCompany() != null) {
			dest.setCompany(orig.getCompany());
		}
		if (orig.getParent() != null) {
			dest.setParent(orig.getParent());
		}
		if (orig.getProjectId() != null) {
			dest.setProjectId(orig.getProjectId());
		}
		if (orig.getConsId() != null) {
			dest.setConsId(orig.getConsId());
		}
		if (orig.getIsApprove() != null) {
			dest.setIsApprove(orig.getIsApprove());
		}
		if (orig.getIsSubmit() != null) {
			dest.setIsSubmit(orig.getIsSubmit());
		}
		if (orig.getStartTime() != null) {
			dest.setStartTime(orig.getStartTime());
		}
		if (orig.getName() != null) {
			dest.setName(orig.getName());
		}
		if (orig.getEndTime() != null) {
			dest.setEndTime(orig.getEndTime());
		}
		if (orig.getPlanDays() != null) {
			dest.setPlanDays(orig.getPlanDays());
		}
		if (orig.getRemark() != null) {
			dest.setRemark(orig.getRemark());
		}
		if (orig.getQuantity() != null) {
			dest.setQuantity(orig.getQuantity());
		}
		if (orig.getManager() != null) {
			dest.setManager(orig.getManager());
		}
		if (orig.getUsername() != null) {
			dest.setUsername(orig.getUsername());
		}
		return dest;
	}

	protected void initDao() {
		// do nothing
	}

	public void save(PmAssignment transientInstance) {
		log.debug("saving PmAssignment instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(PmAssignment persistentInstance) {
		log.debug("deleting PmAssignment instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public PmAssignment findById(java.lang.Integer id) {
		log.debug("getting PmAssignment instance with id: " + id);
		try {
			PmAssignment instance = (PmAssignment) getHibernateTemplate().get(
					"org.ss.psci.entity.PmAssignment", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(PmAssignment instance) {
		log.debug("finding PmAssignment instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName1, Object value1,
			String propertyName2, Object value2) {
		log.debug("finding PmAssignment instance with property: "
				+ propertyName1 + ", value: " + value1);
		try {
			String queryString = "from PmAssignment as model where model."
					+ propertyName1 + "= ? and  model." + propertyName2 + "= ?";
			return getHibernateTemplate().find(queryString,
					new Object[] { value1, value2 });
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding PmAssignment instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from PmAssignment as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByParent(Object parent) {
		return findByProperty(PARENT, parent);
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByStartTime(Object startTime) {
		return findByProperty(START_TIME, startTime);
	}

	public List findByEndTime(Object endTime) {
		return findByProperty(END_TIME, endTime);
	}

	public List findByPlanDays(Object planDays) {
		return findByProperty(PLAN_DAYS, planDays);
	}

	public List findByQuantity(Object quantity) {
		return findByProperty(QUANTITY, quantity);
	}

	public List findByCompany(Object company) {
		return findByProperty(COMPANY, company);
	}

	public List findByManager(Object manager) {
		return findByProperty(MANAGER, manager);
	}

	public List findByPriority(Object priority) {
		return findByProperty(PRIORITY, priority);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findAll() {
		log.debug("finding all PmAssignment instances");
		try {
			String queryString = "from PmAssignment";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PmAssignment merge(PmAssignment detachedInstance) {
		log.debug("merging PmAssignment instance");
		try {
			PmAssignment result = (PmAssignment) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(PmAssignment instance) {
		log.debug("attaching dirty PmAssignment instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(PmAssignment instance) {
		log.debug("attaching clean PmAssignment instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PmAssignmentDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (PmAssignmentDAO) ctx.getBean("PmAssignmentDAO");
	}
}